//
//  MPBankBindController.m
//  MarkPlatform
//
//  Created by xiao6 on 2017/12/22.
//  Copyright © 2017年 AT. All rights reserved.
//

#import "MPBankBindController.h"
#import "MPBankInfoController.h"
#import "MPBankModel.h"
#import "MPInputCell.h"
#import "MPDetailCell.h"
#import "MPInputCellModel.h"

@interface MPBankBindController ()
@property (nonatomic, strong) NSMutableArray<MPInputCellModel *> *dataList;
@property (nonatomic, strong) MPBankModel *bankModel;
@property (nonatomic, strong) MPAppButton *bottomBtn;
@end

@implementation MPBankBindController
+ (instancetype)vcWithModel:(MPBankModel *)bankModel {
    MPBankBindController *vc = [[self alloc] init];
    vc.bankModel = bankModel;
    return vc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showNavTitle:@"绑定银行卡" backItem:YES];
    
    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 400)];
    [footer addSubview:self.bottomBtn];
    [self.bottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bottomBtn.superview.mas_left).offset(16);
        make.centerX.equalTo(self.bottomBtn.superview);
        make.top.equalTo(self.bottomBtn.superview).offset(60);
        make.height.equalTo(@(SCALEW(49.0f)));
    }];
    
    self.tableView.tableFooterView = footer;
}

#pragma mark - action
- (void)didClickBottomBtn:(id)sender {
    if ([self.dataList[0] inputText].length == 0) {
        [MBProgressHUD showMessage:@"请输入姓名"];
        return;
    }
    if ([self.dataList[1] inputText].length < 10) {
        [MBProgressHUD showMessage:@"请输入提现账号"];
        return;
    }
    if ([self.dataList[2] inputText].length < self.dataList[2].maxLength) {
        [MBProgressHUD showMessage:@"请输入身份证号"];
        return;
    }
    [self.view endEditing:YES];
    
    [self.navigationController pushViewController:[MPBankInfoController vcWithModel:self.bankModel] animated:YES];
}
- (void)didClickInfo:(id)sender {
    [ATAlertView showTitle:@"持卡人说明" message:@"为了保证账户资金安全, 只能绑定认证用户本人的银行卡" normalButtons:nil highlightButtons:@[@"知道了"] completion:nil];
}

#pragma mark - tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 64.0f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:16.0f];
    label.textColor = [UIColor colorWithRGB:0x666666];
    label.text = @"请绑定持卡人本人的银行卡";
    [view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(label.superview).offset(12.0f);
        make.bottom.equalTo(label.superview).offset(-20);
    }];
    return view;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MPInputCell *cell = [MPInputCell cellForTableView:tableView indexPath:indexPath];
    if (indexPath.row == 0) {
        cell.accessoryView = [ATHightlightButton buttonWithImage:[UIImage imageNamed:@"icon_xiangxixinxi_nor"] target:self action:@selector(didClickInfo:)];
    }
    else {
        cell.accessoryView = nil;
    }
    cell.model = self.dataList[indexPath.row];
    @weakify(self)
    cell.notifyInputChange = ^(MPInputCell *cell) {
        @strongify(self)
        if (indexPath.row == 0) {
            self.bankModel.userName = cell.model.inputText;
        }
        else if (indexPath.row == 1) {
            self.bankModel.card = cell.model.inputText;
        }
        else if (indexPath.row == 2) {
            self.bankModel.idCard = cell.model.inputText;
        }
    };
    return cell;
}

#pragma mark - getter
- (NSMutableArray<MPInputCellModel *> *)dataList {
    if (!_dataList) {
        _dataList = [NSMutableArray array];
        [_dataList addObject:[MPInputCellModel modelWithTitle:@"姓    名" placeholder:@"请输入姓名" inputText:self.bankModel.name]];
        [_dataList addObject:[MPInputCellModel modelWithTitle:@"账    号" placeholder:@"请输入提现账号" inputText:self.bankModel.card maxLength:30 keyboardType:UIKeyboardTypeNumberPad]];
        [_dataList addObject:[MPInputCellModel modelWithTitle:@"身份证" placeholder:@"请输入身份证号" inputText:self.bankModel.idCard maxLength:18 keyboardType:UIKeyboardTypeDefault]];
    }
    return _dataList;
}
- (UIButton *)bottomBtn {
    if (!_bottomBtn) {
        _bottomBtn = [MPAppButton buttonWithTitle:@"下一步" target:self action:@selector(didClickBottomBtn:)];
    }
    return _bottomBtn;
}

@end
